<?php
session_start ();
$_SESSION ['pathway'] = '..';
$_SESSION ['pathway_js'] = '/fichespataxeurospa';
include ($_SESSION ['pathway'] . "/header.php");
require_once ($_SESSION ['pathway'] . "/commun/ouvre_base.php");

if (! isset ( $_SESSION ['id'] ) or $_SESSION ['acces_clinique'] < 1) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<tr><td><a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></td></tr></table>";
} else {
	
	if (isset ( $_GET ['from'] )) { // c'est une entrée donc puisqu'on garde ancien onglet, pas besoin de fil d'ariane si pas entrée : edit
		$from = $_GET ['from'];
		if ($from != 'entry') {
			?><p>
	<a href="<?php echo $_SESSION['pathway']; ?>/index.php">Home</a> > <a
		href="<?php echo $_SESSION['pathway']; ?>/fichespataxeurospa/choix.php">Clinics</a>
	> Export Clinics
</p><?php
		}
	}
	
	if (isset ( $_GET ['delete'] )) {
		if (isset ( $_GET ['view'] ) and $_GET ['view'] == 'diagnosis') {
			$query_deletediag = "DELETE FROM `spatax`.`diagnostic` WHERE `diagnostic`.`form` = " . $_GET ['delete'];
			mysql_query ( $query_deletediag );
			$query_deletesara = "DELETE FROM `spatax`.`sara` WHERE `sara`.`form` = " . $_GET ['delete'];
			mysql_query ( $query_deletesara );
			$query_deletesprs = "DELETE FROM `spatax`.`sprs` WHERE `sprs`.`form` = " . $_GET ['delete'];
			mysql_query ( $query_deletesprs );
		} else if (isset ( $_GET ['view'] ) and $_GET ['view'] == 'activity') {
			$query_delete_activity = "DELETE FROM `spatax`.`fiche_activite` WHERE `fiche_activite`.`index` = " . $_GET ['delete'];
			mysql_query ( $query_delete_activity );
		}
	}
	
	// fiche clinique
	if (isset ( $_GET ['view'] ) and $_GET ['view'] == 'diagnosis') {
		$requete_total = "SELECT COUNT(*) AS total FROM `diagnostic`";
		$retour_total = mysql_query ( $requete_total ); // Nous récupérons le contenu de la requête dans $retour_total
		$donnees_total = mysql_fetch_assoc ( $retour_total ); // On range retour sous la forme d'un tableau.
		$total = $donnees_total ['total']; // On récupère le total pour le placer dans la variable $total.
		$messagesParPage = 200;
		
		$nombreDePages = ceil ( $total / 200 );
		if (isset ( $_GET ['page'] )) {
			$pageActuelle = intval ( $_GET ['page'] );
			if ($pageActuelle > $nombreDePages) {
				$pageActuelle = $nombreDePages;
			}
		} else {
			$pageActuelle = 1;
		}
		
		$debut_lecture = ($pageActuelle - 1) * $messagesParPage; // On calcul la première entrée à lire
		$requete_finale_form_numbers = "SELECT * FROM `diagnostic`";
		$requete_finale_form_numbers .= " ORDER BY `form` ASC";
		$requete_finale_form_numbers .= " LIMIT $debut_lecture, $messagesParPage;";
		
		$result_recup_entete = mysql_query ( $requete_finale_form_numbers ) or die ( "Impossible de recupérer les ID des forms : " . mysql_error () );
		$liste_IDs = Array ();
		while ( $ligne = mysql_fetch_assoc ( $result_recup_entete ) ) {
			array_push ( $liste_IDs, $ligne ['form'] );
		}
		
		echo "<p><br /><b>" . strtoupper ( 'Diagnosis' ) . "</b><br />";
		echo "<h0>Request : ", $requete_finale_form_numbers, "</h0></p>";
		
		if (count ( $liste_IDs ) != 0) {
			
			echo "Page ";
			for($i = 1; $i <= $nombreDePages; $i ++) {
				if ($i == $pageActuelle) {
					echo ' [ ' . $i . ' ] ';
				} else {
					echo ' <a href="see_clinique.php?from=export&amp;ind=*&amp;view=diagnosis&amp;page=' . $i . '">' . $i . '</a> ';
				}
			}
			
			echo "<br /><br />";
			echo "<table class='principale' cellspacing='1' cellpadding='3' style='margin-top:0px; margin-left:0px; margin-right:0px; font-size:10px; position:relative; float:top; cols=27; '>";
			
			// recuperation champs
			$liste_champs = array ();
			echo "<tr style='color:white; background-color:#c0c0c0;'><th>Modify</th><th>Delete</th>";
			$requete_recup_entete = "SELECT * FROM `diagnostic_nom_champ`
			ORDER BY `ID_index_nom_champ` ASC";
			// echo $requete_recup_entete_linkage;
			$result_recup_entete = mysql_query ( $requete_recup_entete ) or die ( "Impossible de recupérer les entetes : " . mysql_error () );
			while ( $ligne = mysql_fetch_assoc ( $result_recup_entete ) ) {
				echo "<th>", $ligne ['nom_champ'], "</th>";
				array_push ( $liste_champs, $ligne ['champ'] );
			}
			echo "</tr>";
			
			foreach ( $liste_IDs as $ID_form ) {
				// recuperation donnees
				$hash_form_datas = Array ();
				$requete_complete = "SELECT * FROM `diagnostic` WHERE `form` = '$ID_form'";
				$result_clinique = mysql_query ( $requete_complete ) or die ( "Request failed : " . mysql_error () );
				
				while ( $ligne = mysql_fetch_assoc ( $result_clinique ) ) {
					$id_formulaire_ligne = $ligne ['form'];
					if (preg_grep ( "/$id_formulaire_ligne/", $liste_IDs )) {
						// print_r($ligne);
						
						if (isset ( $l ) and $l == 0) {
							$l = 1;
							$style = " style='background-color:#EEF7FC;'";
						} else if (! isset ( $l ) or $l == 1) {
							$l = 0;
							$style = " style='background-color:#E6F2F8;'";
						}
						$j = 0;
						
						foreach ( $liste_champs as $champ ) {
							if ($j == 0) {
								if ($_SESSION ['acces_clinique'] >= 3) {
									echo "<tr$style><td><a href=\"../fichespataxeurospa/page_principale.php?fiche=$ID_form\" style='text-decoration:underline;'><img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='15' /></a></td>";
									echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer.png' height='15' onclick=\"confirmer_suppression_consultation('$ID_form', '$ind', '$from')\" /></td>";
								} else {
									echo "<tr$style><td><img src='" . $_SESSION ['pathway'] . "/images/modifier_grise.png' height='15' /></td>";
									echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer_grise.png' height='15' /></td>";
								}
								$j ++;
							}
							echo "<td style='width:400px; height:30px;'>";
							
							if ($champ == 'nip') {
								echo "<a href=\"../fichespataxeurospa/see_fiche_activite.php?fiche=" . $ligne [$champ] . "\" style='text-decoration:underline;' target=_blank >" . $ligne [$champ] . "</a></td>";
							} elseif ($champ == 'fichiersattaches') {
								echo "<a href=\"../fichespataxeurospa/uploads/" . $ligne [$champ] . "\" style='text-decoration:underline;' target=_blank >" . $ligne [$champ] . "</a></td>";
							} else {
								echo $ligne [$champ] . "</td>";
							}
						}
						echo "</tr>";
					}
				}
			}
			
			$l = 0;
			$liens = array (
					'6' 
			);
			
			?></table><?php
			
			echo "Page ";
			for($i = 1; $i <= $nombreDePages; $i ++) {
				if ($i == $pageActuelle) {
					echo ' [ ' . $i . ' ] ';
				} else {
					echo ' <a href="see_clinique.php?from=export&amp;ind=*&amp;view=diagnosis&amp;page=' . $i . '">' . $i . '</a> ';
				}
			}
		}
	}
	
	// Fiche activité
	if (isset ( $_GET ['view'] ) and $_GET ['view'] == 'activity') {
		
		// modifications
		if (isset ( $_GET ['edit'] )) {
			$_SESSION ['edit'] = $_GET ['edit'];
			require_once ("../fichespataxeurospa/requetes/remplissage_fiche_activite.php");
		}
		
		echo "<p><br /><b>" . strtoupper ( "Activity records" ) . "</b><br />";
		echo "<table class='principale' cellspacing='1' cellpadding='3' style='margin-top:0px; margin-left:0px; margin-right:0px; font-size:10px; position:relative; float:top; cols=27; '>";
		
		$liste_champs = array ();
		echo "<tr style='color:white; background-color:#c0c0c0;'><th>Modify</th><th>Delete</th>";
		$requete_recup_entete = "SELECT * FROM `fiche_activite_nom_champ`
		ORDER BY `ID_index_nom_champ` ASC";
		// echo $requete_recup_entete_linkage;
		$result_recup_entete_ficheactivite = mysql_query ( $requete_recup_entete ) or die ( "Impossible de recupérer les entetes : " . mysql_error () );
		while ( $ligne = mysql_fetch_assoc ( $result_recup_entete_ficheactivite ) ) {
			echo "<th>", $ligne ['nom_champ'], "</th>";
			array_push ( $liste_champs, $ligne ['champ'] );
		}
		echo "</tr>";
		
		if ($l == 0) {
			$l = 1;
			$style = " style='background-color:#EEF7FC;'";
		} else if ($l == 1) {
			$l = 0;
			$style = " style='background-color:#E6F2F8;'";
		}
		
		$requete_fiche_activite = "SELECT * 
		FROM `fiche_activite`
		ORDER BY `index` ASC;";
		$result_fiche_activite = mysql_query ( $requete_fiche_activite ) or die ( "Request failed : " . mysql_error () );
		
		while ( $ligne = mysql_fetch_assoc ( $result_fiche_activite ) ) {
			$j = 0;
			if (is_array ( $ligne )) { // au cas où aucun nip ne corresponde
				echo "<tr$style>";
				foreach ( $liste_champs as $champ ) {
					if ($j == 0) {
						if ($_SESSION ['acces_clinique'] >= 3) {
							echo "<tr$style><td><a href=\"../fichespataxeurospa/see_fiche_activite.php?edit=" . $ligne ['index'] . "&amp;from=entry\" style='text-decoration:underline;'><img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='15' /></a></td>";
							echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer.png' height='15' onclick=\"confirmer_suppression_activity('" . $ligne ['index'] . "', '$ind', '$from')\" /></td>";
						} else {
							echo "<tr$style><td><img src='" . $_SESSION ['pathway'] . "/images/modifier_grise.png' height='15' /></td>";
							echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer_grise.png' height='15' /></td>";
						}
						$j ++;
					}
					echo "<td style='width:400px; height:30px;'>";
					echo $ligne [$champ] . "</td>";
				}
				echo "</tr>";
			}
		}
		
		?></table><?php
	}
}

include ($_SESSION ['pathway'] . "/footer.php");

?>
